<?php

session_start();
setcookie(session_name(), session_id(), time()+3600*24*365, "/");

require_once './include/common.inc.php';
require_once 'left.php';
require_once 'right.php';
include_once './include/cart.class.php';
$page_title = 'Acuity World WebShop';
$menu_id = 0;
$menu_str = PrintMenu();
$check_out = isset($_GET['checkout']) || isset($_GET['modify']) ? 1 : 0;
$cart = new cart();
if(isPromoterLogin()){header("location:shopping_cart_p.php");}
if (isUserLogin()) {
    $sql = "SELECT * FROM  `cus_members` WHERE  `id` = '$webshop_uid'";
    $user_info_ary = $db->get_one($sql);
    $phone = str_replace("||", " " , $user_info_ary['phone']);
    $mobile = str_replace("||", " " , $user_info_ary['mobile']);
    $fax = str_replace("||", " " , $user_info_ary['fax']);
    $faq2_checked = $user_info_ary['faq2'] == 1 ? "checked" : "";
    
    $pay_url = "jump_pay.php";
    
    if (empty($cart->myCart) && isset($_GET['modify'])) {
        if (!$cart = unserialize(getCartInfo($webshop_uid))) {
            $cart = new cart();
        } else {
            $set_country = $cart->getCountry();
            $cart->update();            
        }
    }
	$cart->setSignDate($user_info_ary["date"]);
}


$pid = intval($_GET['pid']);
$act = isset($_GET['act']) ? $_GET['act'] : $_POST['act'];
$vat_u=getVat();
if (isset($_POST['set_country']) &&  $_POST['set_country']>=0 && $_POST['set_country']<=3) {
    $set_country = $_POST['set_country'];
} elseif (isset($_SESSION['myCart_country']) &&  $_SESSION['myCart_country']>=0 && $_SESSION['myCart_country']<=3) {
    $set_country = $_SESSION['myCart_country'];
} else {
    $set_country = 0;
}

// ?
if ($act == 'cart_update') {
    if (!empty($_POST['update_qty_ary'])) {
        foreach ($_POST['update_qty_ary']  as $k=>$v) {
            if (!is_numeric($v)) {
            } else if ($v <= 0) {
                $cart->emptyOne($k);
            } else {
                $cart->modifyCount($k, $v);
            }
        }
    }
}

// hpro_product?
if ($act == 'addone' && $pid>0) {
    $sql = "SELECT `name`, `price`, `cost`, `short_desc`, `vat`, `weight`, `is_special`,`stock_status` FROM  `pro_product` WHERE  `id` = '$pid'";
    $rs = $db->get_one($sql);

    $sql = "SELECT t.`name`, t.`id` FROM  `pro_product_type` pt, `pro_type` t WHERE  pt.`type_id` = t.`id` AND pt.`product_id` = '$pid' GROUP BY t.`id`";
    $query = $db->query($sql);
    while($rs2 = $db->fetch_array($query)) {
        $type_ary_1[$rs2['id']] = $rs2['name'];
    }
    $type_name = '';
    if (!empty($type_ary_1)) {
        $type_name = implode("<br />", $type_ary_1);
    }
	$pro_vat=$rs['vat'];
	if($pro_vat<0)
		$pro_vat=$vat_u;
	if($rs['stock_status']=="1")
	{
    	$cart->setCountry($set_country);		
    	$cart->addOne($pid, $rs['name'], $type_name, $rs['price'], $rs['short_desc'], $pro_vat, $rs);
	}
    header ("location:shopping_cart.php");
    exit;
}

// hcourse_schedule?
if ($act == 'addcourse' && $pid>0) {
    $cart->setCountry($set_country);

    db2Init();
    $sql = "SELECT `name`, `price`, `datedetails`, `date` FROM  `course_schedule` WHERE  `id` = '$pid'";
    $rs = $db2->get_one($sql);
    $rs['weight'] = 0;
	$rs['cost'] = 0;
	
    $date_ary = explode("-", $rs['date']);
    $rs['start_on'] = mktime(0, 0, 0, $date_ary[1], $date_ary[2], $date_ary[0]);

    $cart->addOne($pid, $rs['name'].' (Start on '.$rs['date'].')', 'Courses / Programs', $rs['price'], $rs['datedetails'],$vat_u, $rs);

    header ("location:shopping_cart.php");
    exit;
}

// ?h?
if ($act == 'removeone' && $pid>0) {
    $cart = new cart();
    $cart->emptyOne($pid);
    header ("location:shopping_cart.php");
    exit;
}

$cart->setCountry($set_country);
$cart->update();
$info_ary = $cart->getData();
$total_qty = $cart->getSortCount();
$total_cost = $cart->getTotalCost();
$total_cost2 = $cart->getTotalCost2();
//$cart_addvat = $cart->getAddVat();
$cart_addshippingfee = $cart->getAddShippingFee();
$cart_discount = $cart->getDisCount();
$new_total_cost = number_format(round($total_cost - $cart_discount, 2), 2, '.', '');
$promoter_id = $promoterno = $cart->getPromoterId();
if($promoter_id == "")
{
	$promoter_id=GetCookie("setPromoterId");
}


$cart_addvat=$cart->getAddVat();

require_once PrintEot('header');
require_once PrintEot('left');
require_once PrintEot('shopping_cart');
require_once PrintEot('right');
require_once PrintEot('footer');

?>